<?php
declare (strict_types=1);

namespace app\common\model;

use think\facade\Cache;

class Address extends Base
{
    protected $name = 'user_address';

    public function p()
    {
        return $this->hasOne(Area::class, 'code', 'province')->field('code,province as title');
    }

    public function c()
    {
        return $this->hasOne(Area::class, 'code', 'city')->field('code,city as title');
    }

    public function d()
    {
        return $this->hasOne(Area::class, 'code', 'district')->field('code,district as title');
    }


    public static function add($param, $user): array
    {
        try {
            $type = isset($param['type']) ? $param['type'] : '';
            if (isset($param['default']) && $param['default'] == 1) {
                parent::update(['default' => 0], ['uid' => $user->id]);
            } else {
                $param['default'] = 0;
            }

            $code = 3;
            $url = '';
            if (isset($param['id']) && $param['id']) {
                parent::update($param);
                $msg = '已修改';
            } else {
                $param['uid'] = $user->id;
                unset($param['type']);
                $new = parent::create($param);
                $msg = '已添加';
                if ($type == 'add') {
                    $code = 4;
                    Cache::set('choiceAddress_' . $user->id, $new->id);
                    $url = url('order/index')->build();
                }
            }
            return ['code' => $code, 'msg' => $msg, 'url' => $url];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function edit($id, $user): array
    {
        try {
            $info = parent::where('id', $id)->find();
            if (empty($info) || $info->uid != $user->id) {
                return ['code' => -1, 'msg' => '加载失败，记录不存在'];
            }
            parent::destroy(['id' => $info->id]);
            return ['code' => 1, 'msg' => '已删除'];

        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function getDefault($uid)
    {
        try {
            $cacheId = Cache::get('choiceAddress_' . $uid);

            if ($cacheId) {
                $info = parent::where(['id' => $cacheId])->with(['p', 'c', 'd'])
                    ->withoutField('is_del,create_time,update_time,town')->find();
                if ($info) return $info;
            }
            $info = parent::where(['uid' => $uid, 'default' => 1])->with(['p', 'c', 'd'])
                ->withoutField('is_del,create_time,update_time,town')->find();
            if ($info) return $info;

            $info = parent::where(['uid' => $uid,])->with(['p', 'c', 'd'])
                ->withoutField('is_del,create_time,update_time,town')->order('update_time', 'desc')->find();
            if ($info) return $info;

            return '';
        } catch (\Exception $e) {
            return '';
        }
    }

    public static function choice($id, $user): array
    {
        Cache::set('choiceAddress_' . $user->id, $id);
        return ['code' => 5, 'msg' => 'success'];
    }

}
